package org.amicofragile.test.ws.axis.client;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class EuroClient
{
	public static void main(String[] args)
	{
		if(args.length != 1)
		{
			System.err.println("Necessario URI endpoint");
			System.exit(1);
		}
		EndpointReference targetEndopointReference = new EndpointReference(args[0]);
		try
		{
			System.out.println(targetEndopointReference.getAddress());
			OMFactory fac = OMAbstractFactory.getOMFactory();
			OMNamespace ns = fac.createOMNamespace("http://service.ws.test.amicofragile.org/", "ns1");
			System.out.println("==============================================");
			OMElement payload = fac.createOMElement("buildDescr", ns);
			Options options = new Options();
			ServiceClient client = new ServiceClient();
			options.setTo(targetEndopointReference);
			// Blocking invocation
			client.setOptions(options);
			System.out.println(payload);
			OMElement result = client.sendReceive(payload);
			System.out.println("result = " + result);
			System.out.println("result = " + result.getFirstElement().getText());
			System.out.println("==============================================");
			payload = fac.createOMElement("toEuro", ns);
			
			OMElement value1 = fac.createOMElement("lire", ns);
			//value1.setText("10000");
	        value1.addChild(fac.createOMText(value1, "10000"));
	        payload.addChild(value1);
	        
	        System.out.println(payload);
			result = client.sendReceive(payload);
			System.out.println("result = " + result);
			System.out.println("result = " + result.getFirstElement().getText());
			System.out.println("==============================================");
			payload = fac.createOMElement("adder", ns);
			
			OMElement a = fac.createOMElement("a", ns);
	        a.addChild(fac.createOMText(a, "11"));
	        payload.addChild(a);
	        
	        OMElement b = fac.createOMElement("b", ns);
	        b.addChild(fac.createOMText(b, "19"));
	        payload.addChild(b);
	        
	        System.out.println(payload);
			result = client.sendReceive(payload);
			System.out.println("result = " + result);
			System.out.println("result = " + result.getFirstElement().getText());
			System.out.println("==============================================");
		} catch (AxisFault axisFault)
		{
			axisFault.printStackTrace();
		}

	}
}